package com.yiwu.web.controller;

import com.google.common.base.Strings;
import com.yiwu.common.aspect.ApiOperationLog;
import com.yiwu.common.aspect.ApiVerifyLogin;
import com.yiwu.common.enums.ResponseCode;
import com.yiwu.common.exception.BaseResponse;
import com.yiwu.common.exception.BusinessException;
import com.yiwu.common.model.vo.product.*;
import com.yiwu.common.utils.ResultUtil;
import com.yiwu.web.service.ProductsService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.yiwu.common.utils.EncryptionUtil.getUserToken;

/**
 * @version 1.0
 * @description TODO
 * @date 2024/5/18 23:09
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductsService productsService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "上架自己的商品")
    @ApiOperationLog
    @ApiVerifyLogin
    @PostMapping("/add")
    public BaseResponse<String> addProduct(@RequestBody ProductAddReqVO productAddReqVO, HttpServletRequest request) {
        if (Objects.isNull(productAddReqVO)) {
            throw new BusinessException(ResponseCode.PARAMETER_ERROR);
        }
        Long sellerId = getUserToken(request);
        if (productAddReqVO.getImageUrl().length > 9) throw new BusinessException(ResponseCode.PARAMETER_ERROR);
        String result = productsService.addProduct(productAddReqVO, sellerId);
        String productCount = stringRedisTemplate.opsForValue().get(sellerId + "yiwu-productCount");
        if (productCount != null) {
            stringRedisTemplate.opsForValue().set(sellerId + "yiwu-productCount", String.valueOf((Integer.parseInt(productCount) + 1)), 7, TimeUnit.DAYS);
        }
        return ResultUtil.success(result);
    }

    @Operation(summary = "查看一个商品")
    @GetMapping("/get/{productId}")
    public BaseResponse<ProductImageRspVO> getProductDetailsById(@PathVariable String productId) {
        if (Strings.isNullOrEmpty(productId)) {
            throw new BusinessException(ResponseCode.PARAMETER_ERROR);
        }
        ProductImageRspVO productImageDTO = productsService.getProductDetailsById(productId);
        return ResultUtil.success(productImageDTO);
    }

    @Operation(summary = "首页分页查询")
    @PostMapping("/get")
    public BaseResponse<List<ProductAllRspVO>> getAllProduct(@RequestBody ProductAllReqVO productAllReqVO) {
        List<ProductAllRspVO> allProduct = productsService.getAllProduct(productAllReqVO);
        return ResultUtil.success(allProduct);
    }

    @ApiOperationLog
    @ApiVerifyLogin
    @Operation(summary = "更改商品状态为下架")
    @PostMapping("/update/sold")
    public BaseResponse<Boolean> updateProductSold(@RequestBody ProductReqVO productReqVO, HttpServletRequest request) {
        Long sellerId = getUserToken(request);
        Boolean result = productsService.updateProductStatus(productReqVO, 2, sellerId);
        String productCount = stringRedisTemplate.opsForValue().get(sellerId + "yiwu-productCount");
        if (productCount != null) {
            stringRedisTemplate.opsForValue().set(sellerId + "yiwu-productCount", String.valueOf((Integer.parseInt(productCount) - 1)), 7, TimeUnit.DAYS);
        }
        return ResultUtil.success(result);
    }

    @ApiOperationLog
    @ApiVerifyLogin
    @Operation(summary = "更改商品状态为上架")
    @PostMapping("/update/available")
    public BaseResponse<Boolean> updateProductAvailable(@RequestBody ProductReqVO productReqVO, HttpServletRequest request) {
        Long sellerId = getUserToken(request);
        Boolean result = productsService.updateProductStatus(productReqVO, 0, sellerId);
        String productCount = stringRedisTemplate.opsForValue().get(sellerId + "yiwu-productCount");
        if (productCount != null) {
            stringRedisTemplate.opsForValue().set(sellerId + "yiwu-productCount", String.valueOf((Integer.parseInt(productCount) + 1)), 7, TimeUnit.DAYS);
        }
        return ResultUtil.success(result);
    }

    @ApiOperationLog
    @ApiVerifyLogin
    @Operation(summary = "更改商品状态为删除")
    @PostMapping("/update/delete")
    public BaseResponse<Boolean> updateProductDelete(@RequestBody ProductReqVO productReqVO, HttpServletRequest request) {
        Long sellerId = getUserToken(request);
        Boolean result = productsService.updateProductStatus(productReqVO, 1, sellerId);
        String productCount = stringRedisTemplate.opsForValue().get(sellerId + "yiwu-productCount");
        if (productCount != null) {
            stringRedisTemplate.opsForValue().set(sellerId + "yiwu-productCount", String.valueOf((Integer.parseInt(productCount) - 1)), 7, TimeUnit.DAYS);
        }
        return ResultUtil.success(result);
    }

    @ApiOperationLog
    @ApiVerifyLogin
    @Operation(summary = "查看自己发布的商品")
    @PostMapping("/my/publish")
    public BaseResponse<List<ProductAllRspVO>> getMyProductList(@RequestBody ProductAllReqVO productAllReqVO, HttpServletRequest request) {
        Long userId = getUserToken(request);
        List<ProductAllRspVO> productAllRspVO = productsService.getMyProductList(productAllReqVO, userId);
        return ResultUtil.success(productAllRspVO);
    }

    @ApiOperationLog
    @Operation(summary = "查看卖家的其他商品")
    @PostMapping("/seller/other")
    public BaseResponse<List<ProductAllRspVO>> getSellerProductList(@RequestBody ProductSellerReqVO productSellerReqVO, HttpServletRequest request) {
        List<ProductAllRspVO> sellerProductList = productsService.getSellerProductList(productSellerReqVO);
        return ResultUtil.success(sellerProductList);
    }

    @ApiOperationLog
    @Operation(summary = "查看推荐商品")
    @PostMapping("/recommend")
    public BaseResponse<List<ProductAllRspVO>> getRecommendProductList(@RequestParam String productId, HttpServletRequest request) {
        List<ProductAllRspVO> recommendProductList = productsService.getRecommendProductList(productId);
        return ResultUtil.success(recommendProductList);
    }

    //todo 商品搜索功能使用es
    @GetMapping("/search/{keyword}")
    public BaseResponse<List<String>> searchKeySuggest(@PathVariable String keyword) {
        List<String> res = productsService.searchKeySuggest(keyword);
        return ResultUtil.success(res);
    }


}
